package com.taijihuabao.common.rabbitmq;

import cn.hutool.core.util.IdUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.taijihuabao.common.emailutils.EmailPojo;
import com.taijihuabao.common.exception.ServiceException;
import com.taijihuabao.common.jsonresult.ApplicationResponseCode;
import com.taijihuabao.common.commonutils.RandomValidateCodeUtil;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author changhui.tu
 * mq生产消息
 */
@Component
public class RabbitMqProducer {

    /**
     * 过期时间(分)
     */
    public static final long OUT_TIME = 5L;
    public static int REGISTER_VAIL_CODE;
    public static int FORGET_VAIL_CODE;
    private final String MAIL_SUBJECT = "【用户考勤管理系统】";


    @Autowired
    RedisTemplate<String, Object> redisTemplate;
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private ObjectMapper objectMapper;

    /**
     * 用户注册验证码
     *
     * @param emailPojo
     */
    public void mailRegisterVailCodeSend(EmailPojo emailPojo) {
        int random = RandomValidateCodeUtil.sixMathValidateCode();
        REGISTER_VAIL_CODE = random;
        String registerVailCode = "【实习吧】验证码：" + random + "，您正在注册账号，该验证码在" + OUT_TIME + "分钟内有效。";
        emailPojo.setSubject(MAIL_SUBJECT);
        emailPojo.setContent(registerVailCode);
        commonMailProducer(emailPojo);
    }

    /**
     * 忘记密码验证码
     *
     * @param emailPojo
     */
    public void mailForgetVailCodeSend(EmailPojo emailPojo) {
        int random = RandomValidateCodeUtil.sixMathValidateCode();
        FORGET_VAIL_CODE = random;
        String forgetVailCode = "【实习吧】验证码：" + random + "，您正在找回密码，" + OUT_TIME + "分钟内有效。切勿将验证码告知他人，以防账号被盗。";
        emailPojo.setSubject(MAIL_SUBJECT);
        emailPojo.setContent(forgetVailCode);
        commonMailProducer(emailPojo);
    }

    /**
     * 邮箱发送公共生产者
     *
     * @param emailPojo
     */
    public void commonMailProducer(EmailPojo emailPojo) {
        try {
            //将序列化对象转换为字节数组,并设置唯一id
            Message msg = MessageBuilder.withBody(objectMapper.writeValueAsBytes(emailPojo)).setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT).setMessageId(IdUtil.fastSimpleUUID()).build();
            //设置消息转换器
            rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
            //发送消息: 交换机、路由key、数据
            rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_MESSAGE_SEND, RabbitmqConfig.ROUTING_KEY_EMAIL, msg);
        } catch (Exception e) {
            throw new ServiceException(ApplicationResponseCode.INTER_ERROR);
        }
    }

    /**
     * 消息重发
     *
     * @param messageId
     * @param emailPojo
     */
    public void resendMessageCommon(String messageId, EmailPojo emailPojo) {
        try {
            //将序列化对象转换为字节数组,并设置id
            Message msg = MessageBuilder.withBody(objectMapper.writeValueAsBytes(emailPojo)).setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT).setMessageId(messageId).build();
            //设置消息转换器
            rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
            //发送消息: 交换机、路由key、数据
            rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_MESSAGE_SEND, RabbitmqConfig.ROUTING_KEY_EMAIL, msg);
        } catch (Exception e) {
            throw new ServiceException(ApplicationResponseCode.INTER_ERROR);
        }
    }

}
